import { ref, computed } from 'vue'
import { defineStore } from 'pinia'//定义一个store

// conversations: 对话数组，每个对话包含id、title、messages、createdAt
// currentConversationId: 当前选中的对话ID
// currentConversation: 当前选中的对话
// currentMessages: 当前选中的对话的消息
// createConversation: 创建新对话
// switchConversation: 切换对话
// addMessage: 添加消息到当前对话
// isLoading: 是否处于AI回复中状态
// setIsLoading: 设置AI回复中状态
// updateLastMessage: 更新最新消息
// getLastMessage: 得到最新消息
// deleteConversation: 删除对话
// updateTitleFromMessage: 从消息内容更新标题

// 定义消息类型
interface Message {
    id: number;
    role: 'user' | 'assistant'; // 消息角色：用户/助手
    content: string;            // 消息内容
    reasoning_content: string;  // 推理内容（如AI解释）
    files: File[];              // 附件
    completion_tokens?: string; // 生成token数（可选）
    speed: string;              // 回复速度
}
// 定义对话类型
interface Conversation {
    id: string;           // 会话唯一ID
    title: string;        // 会话标题
    messages: Message[];  // 消息数组,子元素是Messa类型
    createdAt: number;    // 创建时间戳
}


//创建叫useChatStore的pinia仓库  llm-chat:这个store的id，标识这个 Store
export const useChatStore = defineStore('llm-chat',
    () => {
        const conversations = ref<Conversation[]>([
            {
                id: "1",
                title: '日常问候',
                messages: [],
                createdAt: Date.now(),
            }
        ])
        // 当前选中的对话 ID
        const currentConversationId = ref("1")

        // 获取当前对话
        const currentConversation = computed(() => {
            return conversations.value.find((conv) => conv.id === currentConversationId.value)
        })

        // 获取当前对话的消息,如果currentConversation.value为null或undefined，或者没有messages属性,返回空数组
        const currentMessages = computed(() => currentConversation.value?.messages || [])

        // 创建新对话
        const createConversation = () => {
            const newConversation = {
                id: Date.now().toString(),
                title: '日常问候',
                messages: [],
                createdAt: Date.now(),
            }
            conversations.value.unshift(newConversation)//从数组前面添加
            currentConversationId.value = newConversation.id
        }

        // 切换对话
        const switchConversation = (conversationId: string) => {
            currentConversationId.value = conversationId
        }

        // 添加消息到当前对话
        const addMessage = (message: Message) => {
            if (currentConversation.value) {
                currentConversation.value.messages.push({
                    ...message
                })
            }
        }

        // 加载状态
        const isLoading = ref(false)

        //更改加载状态
        const setIsLoading = (value: boolean) => {
            isLoading.value = value
        }

        //更新最新消息,将得到的response复制到Conversation数组中
        const updateLastMessage = (
            content: string, reasoning_content: string, completion_tokens: string, speed: string) => {
            //先检查messages.length存不存在，再确认是否>0
            if (currentConversation.value?.messages.length && currentConversation.value?.messages.length > 0) {
                const lastMessage = currentConversation.value.messages[currentConversation.value.messages.length - 1]
                lastMessage.content = content
                lastMessage.reasoning_content = reasoning_content
                lastMessage.completion_tokens = completion_tokens
                lastMessage.speed = speed
            }
        }

        //得到最新message
        const getLastMessage = () => {
            if (currentConversation.value?.messages.length && currentConversation.value?.messages.length > 0) {
                return currentConversation.value.messages[currentConversation.value.messages.length - 1]
            }
            return null
        }

        // 从消息内容更新标题
        const updateTitleFromMessage = (content: string) => {
            if (!currentConversation.value) return
            // 去除 Markdown 标记
            // 提取前6个字符作为标题
            let newTitle = content.trim().substring(0, 6)
            // 添加省略号如果内容被截断
            if (content.length > 6) {
                newTitle += '...'
            }
            // 确保标题不为空
            if (!newTitle) {
                newTitle = '日常问候'
            }

            currentConversation.value.title = newTitle
        }

        // 删除对话
        const deleteConversation = (conversationId: string) => {
            const index = conversations.value.findIndex(conversation =>
                conversation.id === conversationId
            )
            if (index !== -1) {
                //在数组中删除
                conversations.value.splice(index, 1)
                // 如果删除后没有对话了，创建一个新对话
                if (conversations.value.length === 0) {
                    createConversation()
                }
                // 如果删除的是当前对话，切换到第一个对话
                else if (conversationId === currentConversationId.value) {
                    currentConversationId.value = conversations.value[0].id
                }

            }
        }


        return {
            conversations,
            currentConversationId,
            currentConversation,
            currentMessages,
            createConversation,
            switchConversation,
            addMessage,
            isLoading,
            setIsLoading,
            updateLastMessage,
            getLastMessage,
            deleteConversation,
            updateTitleFromMessage
        }// 暴露状态供组件使用
    },
    {
        persist: true,
    },
)